package others.swing.p022;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;

class AbstractEvent extends AbstractAction {
    private String name = null;
    private MyFrame frame = null;
    private MyService service = MyService.getInstance();

    AbstractEvent(String name, Icon icon, MyFrame frame) {
        super(name, icon);
        this.name = name;
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //弹出确认对话框
        if (e.getActionCommand() == "打开") {
            service.openFile(this.frame);
        } else if (e.getActionCommand() == "上一页") {
            service.lastImage(this.frame);
        } else if (e.getActionCommand() == "下一页") {
            service.nextwImage(this.frame);
        } else if (e.getActionCommand() == "放大") {
            service.zoomBigger(this.frame);
        } else if (e.getActionCommand() == "缩小") {
            service.zoomSmall(this.frame);
        }
    }
}

public class MyFrame extends JFrame {
    private int width = 1050;   //窗口的宽度
    private int height = 700;  //窗口的高度
    private int pX = 40;
    private int pY = 10;
    private JLabel label = new JLabel();
    private JPanel panel;
    private MyService service = MyService.getInstance();
    private ActionListener menuListener;

    public MyFrame() {
        menuListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                service.funcOfImage(MyFrame.this, e.getActionCommand());
            }
        };
        initFrame();
    }

    void initFrame() {
        this.setTitle("图像浏览器");
        this.setLocation(pX, pY);
        this.setPreferredSize(new Dimension(width, height));  //设置窗口大小
        createMenuBar();
        createToolPanel();
        this.add(new JScrollPane(label), BorderLayout.CENTER);
        this.setVisible(true);
        this.pack();
    }

    void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        String menuArray[] = {"文件", "工具", "帮助"};
        String menuArrayItem[][] = {
                {"打开", "退出"},
                {"上一页", "下一页", "放大", "缩小"},
                {"帮助"}
        };
        for (int i = 0; i < menuArray.length; ++i) {
            JMenu menu = new JMenu(menuArray[i]);
            for (int j = 0; j < menuArrayItem[i].length; ++j) {
                JMenuItem menuItem = new JMenuItem(menuArrayItem[i][j]);
                menuItem.addActionListener(menuListener);
                menu.add(menuItem);
            }
            menuBar.add(menu);
        }
        this.setJMenuBar(menuBar);
    }

    void createToolPanel() {
        panel = new JPanel();
        JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(true);  //实现工具栏的不浮动

        String toolImgArr[] = {"打开",
                "上一页",
                "下一页",
                "放大",
                "缩小"};
        for (int i = 0; i < toolImgArr.length; ++i) {
            ImageIcon imageIcon = new ImageIcon("src/img/" + toolImgArr[i] + ".gif");
            AbstractEvent absAction = new AbstractEvent(toolImgArr[i], imageIcon, this);
            JButton btnTool = new JButton(absAction);
            btnTool.setVerticalTextPosition(JButton.BOTTOM);
            btnTool.setHorizontalTextPosition(JButton.CENTER);
            toolBar.add(btnTool);
        }
        panel.add(toolBar);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.add(panel, BorderLayout.NORTH);
    }

    public JLabel getLabel() {
        return label;
    }
}
